import type { VbenFormSchema } from '#/adapter/form';
import type { OnActionClickFn, VxeTableGridOptions } from '#/adapter/vxe-table';
import type { AccountBalanceApi } from '#/api/reservation/account-balance';

import { useAccess } from '@vben/access';

import { getRangePickerDefaultProps } from '#/utils';

const { hasAccessByCodes } = useAccess();

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'memberId',
      label: '会员id',
      component: 'Input',
      componentProps: {
        placeholder: '请输入会员id',
      },
    },
    {
      fieldName: 'totalBalance',
      label: '总余额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入总余额',
      },
    },
    {
      fieldName: 'cashBalance',
      label: '现金金额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入现金金额',
      },
    },
    {
      fieldName: 'giftBalance',
      label: '赠送余额',
      component: 'Input',
      componentProps: {
        placeholder: '请输入赠送余额',
      },
    },
    {
      fieldName: 'lastUpdate',
      label: '最后更新时间',
      component: 'DatePicker',
      componentProps: {
        showTime: true,
        format: 'YYYY-MM-DD HH:mm:ss',
        valueFormat: 'x',
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'memberId',
      label: '会员id',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入会员id',
      },
    },
    {
      fieldName: 'totalBalance',
      label: '总余额',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入总余额',
      },
    },
    {
      fieldName: 'cashBalance',
      label: '现金金额',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入现金金额',
      },
    },
    {
      fieldName: 'giftBalance',
      label: '赠送余额',
      component: 'Input',
      componentProps: {
        allowClear: true,
        placeholder: '请输入赠送余额',
      },
    },
    {
      fieldName: 'lastUpdate',
      label: '最后更新时间',
      component: 'RangePicker',
      componentProps: {
        ...getRangePickerDefaultProps(),
        allowClear: true,
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(
  onActionClick?: OnActionClickFn<AccountBalanceApi.AccountBalance>,
): VxeTableGridOptions<AccountBalanceApi.AccountBalance>['columns'] {
  return [
    {
      field: 'id',
      title: '主键',
      minWidth: 120,
    },
    {
      field: 'memberId',
      title: '会员id',
      minWidth: 120,
    },
    {
      field: 'totalBalance',
      title: '总余额',
      minWidth: 120,
    },
    {
      field: 'cashBalance',
      title: '现金金额',
      minWidth: 120,
    },
    {
      field: 'giftBalance',
      title: '赠送余额',
      minWidth: 120,
    },
    {
      field: 'lastUpdate',
      title: '最后更新时间',
      minWidth: 120,
      formatter: 'formatDateTime',
    },
    {
      field: 'operation',
      title: '操作',
      minWidth: 200,
      align: 'center',
      fixed: 'right',
      headerAlign: 'center',
      showOverflow: false,
      cellRender: {
        attrs: {
          nameField: 'id',
          nameTitle: '会员余额',
          onClick: onActionClick,
        },
        name: 'CellOperation',
        options: [
          {
            code: 'edit',
            show: hasAccessByCodes(['reservation:account-balance:update']),
          },
          {
            code: 'delete',
            show: hasAccessByCodes(['reservation:account-balance:delete']),
          },
        ],
      },
    },
  ];
}
